﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Xml.Linq;
using SFP.Audio;

namespace SFP
{
    public class ContentLoader
    {
        private const int BITMAP = 0;
        private const int AUDIO = 1;
        private const int XML = 2;

        internal static string startupPath;

        public static Bitmap LoadBitmap(string file)
        {
            return (Bitmap)Load(BITMAP, file);
        }

        public static Sound LoadAudio(string file)
        {
            return (Sound)Load(AUDIO, file);
        }

        public static XDocument LoadXML(string file)
        {
            return (XDocument)Load(XML, file);
        }

        private static Object Load(int type, string file)
        {
            if (File.Exists(startupPath + file))
            {
                try
                {
                    if (type == BITMAP)
                    {
                        Bitmap orgbitmap = new Bitmap(startupPath + file);
                        orgbitmap.SetResolution(96.0f, 96.0f);

                        Bitmap bitmap = new Bitmap(orgbitmap.Width, orgbitmap.Height, PixelFormat.Format32bppPArgb);
                        Graphics.FromImage(bitmap).DrawImage(orgbitmap, 0, 0);

                        return bitmap;
                    }
                    if (type == AUDIO)
                        return new Sound(startupPath + file);
                    if (type == XML)
                        return XDocument.Load(startupPath + file);
                }
                catch (ArgumentException)
                {
                    return null;
                }
            }

            return null;
        }
    }
}
